import 'package:flutter/material.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
import 'package:intl/intl.dart';
import 'package:seedfour/app/constants.dart';
import 'package:seedfour/app/views/module_seal/seal_detail/state.dart';

import '../../../../models/seal_detail_entity.dart';

class ApproveListWidget extends StatelessWidget {
  const ApproveListWidget({
    Key? key,
    required this.data,
  }) : super(key: key);

  final SealDetailState data;

  @override
  Widget build(BuildContext context) {
    return _buildBg(children: [
      // 审批信息
      _buildApproveItemBg(builder: (item) {
        return [
          _buildApproveItem(item: item),
        ];
      }),
    ]);
  }

  Widget _renderRow(String title, String content) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(title,
            style: TextStyle(fontSize: kFontSizeSmall, color: Colors.grey)),
        Text(content, style: TextStyle(fontSize: kFontSizeMedium)),
      ],
    );
  }

  Widget _buildApproveItem({required SealApproveRecord item}) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _renderRow('审批类型：', approveType[item.type!] ?? ''),
        SizedBox(height: kSpaceTiny),
        _renderRow(
            '审批结果：',
            item.type != 4
                ? approveStatus[item.approve!] ?? ''
                : sealStatus[item.approve!] ?? ''),
        SizedBox(height: kSpaceTiny),
        _renderRow('审批人：', item.approverName ?? ''),
        SizedBox(height: kSpaceTiny),
        _renderRow(
          '申请时间：',
          item.createdAt == null
              ? ''
              : DateFormat('yyyy-MM-dd HH:mm')
                  .format(DateTime.fromMillisecondsSinceEpoch(item.createdAt!)),
        ),
        SizedBox(height: kSpaceTiny),
        _renderRow(
          '审批时间：',
          item.updatedAt == null
              ? ''
              : DateFormat('yyyy-MM-dd HH:mm')
                  .format(DateTime.fromMillisecondsSinceEpoch(item.updatedAt!)),
        ),
        SizedBox(height: kSpaceTiny),
        _renderRow('备注：', item.remark ?? ''),
      ],
    );
  }

  Widget _buildApproveItemBg(
      {required List<Widget> Function(SealApproveRecord item) builder}) {
    return Obx(() {
      return Column(
        children: List.generate(
          data.sealApproveList.length,
          (index) => Column(
            children: [
              ...builder(data.sealApproveList[index]),
              SizedBox(height: kSpaceSmall),
              index < data.sealApproveList.length - 1
                  ? const Divider()
                  : Container(),
              index < data.sealApproveList.length - 1
                  ? SizedBox(height: kSpaceSmall)
                  : Container(),
            ],
          ),
        ),
      );
    });
  }

  Widget _buildBg({required List<Widget> children}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          margin: EdgeInsets.only(left: kSpaceMedium, top: kSpaceSmall),
          child: Text(
            '审批处理过程',
            style: TextStyle(fontSize: kFontSizeLarge, color: Colors.black87),
          ),
        ),
        Container(
          margin: EdgeInsets.all(kSpaceSmall),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(kRadiusSmall),
            color: Colors.white,
          ),
          padding: EdgeInsets.only(
              left: kSpaceMedium, right: kSpaceMedium, top: kSpaceSmall),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: children,
          ),
        )
      ],
    );
  }
}
